package com.IOSLab.controller;


import com.IOSLab.common.Const;
import com.IOSLab.common.HttpServletRequestUtil;
import com.IOSLab.common.ServerResponse;
import com.IOSLab.dto.UserInfoExecution;
import com.IOSLab.entity.UserInfo;
import com.IOSLab.enums.UserInfoEnum;
import com.IOSLab.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/user/")
public class UserInfoController {
    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * create:LIUYANG
     * description:用户登录
     *
     * @Date:2018/12/15
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public  Map<String, Object> login(HttpSession session,HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String username = HttpServletRequestUtil.getString(request, "username");
        String password_1 = HttpServletRequestUtil.getString(request, "password_1");
        UserInfoExecution logincheck = iUserInfoService.Login(username,password_1);
        if (logincheck.getState() == UserInfoEnum.SUCCESS.getState()) {
            modelMap.put("success", true);

        }else {
            modelMap.put("success", false);
            modelMap.put("errMsg", logincheck.getStateInfo());
        }
        return  modelMap;
//        UserInfoExecution response = iUserInfoService.Login(username, password);
//        if (response.isSuccess()) {
//            session.setAttribute(Const.CURRENT_USER, response.getData());
//        }
//        return response;
    }

//
//    @RequestMapping(value = "logout", method = RequestMethod.GET)
//    @ResponseBody
//    public ServerResponse<UserInfo> logout( HttpSession session) {
//        session.removeAttribute(Const.CURRENT_USER);
//        ServerResponse<UserInfo> bySuccess = ServerResponse.createBySuccess();
//        return bySuccess;
//    }


    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object>  register( UserInfo userInfo,HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String username = HttpServletRequestUtil.getString(request, "username");
        String password_1 = HttpServletRequestUtil.getString(request, "password_1");
        String password_2 = HttpServletRequestUtil.getString(request, "password_2");
        String student_id =HttpServletRequestUtil.getString(request,"student_id");
        String nickname =HttpServletRequestUtil.getString(request,"nickname");
        String password =HttpServletRequestUtil.getString(request,"passowrd");
        String phone =HttpServletRequestUtil.getString(request,"phone");
        String head_view =HttpServletRequestUtil.getString(request,"head_view");
        String part_type =HttpServletRequestUtil.getString(request,"part_type");
        String git_name =HttpServletRequestUtil.getString(request,"git_name");
        if (username.equals(null)||password_1.equals(null)||password_2.equals(null)){
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入完整参数");
        }else {
            try {
                userInfo.setStudentId(student_id);
                userInfo.setPassword(password);
                userInfo.setNickname(nickname);
                userInfo.setPassword(phone);
                userInfo.setPassword(head_view);
                userInfo.setPassword(part_type);
                userInfo.setPassword(git_name);
                UserInfoExecution register = iUserInfoService.Register(userInfo);
                if (register.getState() == UserInfoEnum.SUCCESS.getState()) {
                    modelMap.put("success", true);
                } else {
                    modelMap.put("success", false);
                    modelMap.put("errMsg", register.getStateInfo());
                }
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }
        return modelMap;
    }

//    @RequestMapping(value = "", method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse<UserInfo>getUserInfo(HttpSession session) {
//        UserInfo userInfo=(UserInfo)session.getAttribute(Const.CURRENT_USER);
//        if(userInfo!=null){
//            return  ServerResponse.createBySuccess(userInfo);
//        }
//        return ServerResponse.createByErrorMessage("用户未登录，无法获取当前信息");
//
//    }

}
